//
//  JXHistoryInfoView.m
//

#import "JXHistoryInfoView.h"

#import "JXHistoryItemTableCell.h"

#define SelfFrameSizeWidth self.frame.size.width
#define SelfFrameSizeHeight self.frame.size.height
#define BgViewFrameWidth self.backgroundView.frame.size.width
#define BgViewFrameHeight self.backgroundView.frame.size.height

#define SpaceSize 20
#define HeaderSizeWidth 40
#define titleLabelSizeWidth 160
#define titleLabelSizeHeight 30

@interface JXHistoryInfoView ()<UITableViewDataSource, UITableViewDelegate,
                                JXHistoryItemTableCellDelegate>

@property(nonatomic, copy) NSString *title;
@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) UIView *headerView;
@property(nonatomic, strong) UIView *leftView;
@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UITableView *itemTableView;
@property(nonatomic, strong) NSArray *items;

@end

@implementation JXHistoryInfoView

- (instancetype)initWithTtile:(NSString *)title
                     delegate:(id<JXHistoryInfoViewDelegate>)delegate
                        items:(NSArray *)items {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,
                                [UIScreen mainScreen].bounds.size.height);
        self.title = title;
        self.items = items;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
        if (delegate) {
            self.delegate = delegate;
        }
        [self backgroundView];
        [self headerView];
        [self leftView];
        [self contentView];
        [self titleLabel];
        [self itemTableView];
    }
    return self;
}

- (void)showInView:(UIView *)view {
    //添加一个动画
    [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
}

- (void)hideView {
    [UIView animateWithDuration:0.25f
            animations:^{
                [self setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, 0,
                                          [UIScreen mainScreen].bounds.size.height)];
            }
            completion:^(BOOL finished) {
                if (finished) {
                    [self removeFromSuperview];
                }
            }];
}

#pragma mark getter methods

- (UIView *)leftView {
    if (!_leftView) {
        _leftView = [[UIView alloc]
                initWithFrame:CGRectMake(0, 0, 0.25 * SelfFrameSizeWidth, SelfFrameSizeHeight)];
        _leftView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap =
                [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView)];
        [_leftView addGestureRecognizer:tap];
        [self addSubview:_leftView];
    }
    return _leftView;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        CGFloat NavHeight = 64.f;
        _backgroundView =
                [[UIView alloc] initWithFrame:CGRectMake(0.25 * SelfFrameSizeWidth, NavHeight,
                                                         0.75 * SelfFrameSizeWidth,
                                                         (SelfFrameSizeHeight - NavHeight))];
        _backgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_backgroundView];
    }
    return _backgroundView;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView =
                [[UIView alloc] initWithFrame:CGRectMake(0, 0, BgViewFrameWidth, HeaderSizeWidth)];
        _headerView.backgroundColor = [UIColor whiteColor];
        [self.backgroundView addSubview:_headerView];
    }
    return _headerView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        CGFloat space = 5.f;
        CGFloat height = 30.f;
        _titleLabel = [[UILabel alloc]
                initWithFrame:CGRectMake((BgViewFrameWidth - titleLabelSizeWidth) / 2, space,
                                         titleLabelSizeWidth, height)];
        _titleLabel.text = self.title;
        _titleLabel.font = [UIFont systemFontOfSize:18.f];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.headerView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UITableView *)itemTableView {
    if (!_itemTableView) {
        _itemTableView = [[UITableView alloc]
                initWithFrame:CGRectMake(0, 1, self.contentView.frame.size.width,
                                         self.contentView.frame.size.height - 1)
                        style:UITableViewStyleGrouped];
        _itemTableView.dataSource = self;
        _itemTableView.delegate = self;

        [self.contentView addSubview:_itemTableView];
    }
    return _itemTableView;
}

- (UIView *)contentView {
    if (!_contentView) {
        CGFloat origionY = CGRectGetMaxY(self.headerView.frame) + 5;
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, origionY, BgViewFrameWidth,
                                                                BgViewFrameHeight - origionY)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self.backgroundView addSubview:_contentView];
    }
    return _contentView;
}

#pragma mark JXHistoryItemTableCellDelegate

- (void)sendItemInfo:(JXItemModel *)item {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedInfoWithItem:)]) {
        [self.delegate didSelectedInfoWithItem:item];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)),
                       dispatch_get_main_queue(), ^{
                           [self hideView];
                       });
    }
}

#pragma mark UITableViewDataSource & UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CGRectGetHeight(tableView.frame) / 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"comment";
    JXItemModel *item = self.items[indexPath.row];
    JXHistoryItemTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[JXHistoryItemTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                             reuseIdentifier:CellIdentifier
                                                       model:item
                                                    delegate:self];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001f;
}

@end
